package com.shanzhu.flower.dao;

import com.shanzhu.flower.entity.Purchase;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface PurchaseDao {
    @Select("select * from purchase order by id desc")
    List<Purchase> findAll();

    @Insert("insert into purchase(product, quantity, price, total, supplier, date) values(#{product}, #{quantity}, #{price}, #{total}, #{supplier}, #{date})")
    int add(Purchase purchase);

    @Update("update purchase set product=#{product}, quantity=#{quantity}, price=#{price}, total=#{total}, supplier=#{supplier}, date=#{date} where id=#{id}")
    int update(Purchase purchase);

    @Delete("delete from purchase where id=#{id}")
    int delete(int id);

    @Select("<script>"
        + "select * from purchase "
        + "<where>"
        + "  <if test='searchKey != null and searchKey != \"\"'>"
        + "    and (product like concat('%',#{searchKey},'%') or supplier like concat('%',#{searchKey},'%'))"
        + "  </if>"
        + "</where>"
        + "order by id asc limit #{offset}, #{pageSize}"
        + "</script>")
    List<Purchase> findPage(@Param("searchKey") String searchKey, @Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("<script>"
        + "select count(*) from purchase "
        + "<where>"
        + "  <if test='searchKey != null and searchKey != \"\"'>"
        + "    and (product like concat('%',#{searchKey},'%') or supplier like concat('%',#{searchKey},'%'))"
        + "  </if>"
        + "</where>"
        + "</script>")
    int count(@Param("searchKey") String searchKey);
} 